package demo.bean;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

import demo.entrymain.EntryMain;
import demo.factor.Ifactor.IFactory;

public class Client implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -6143474194815121598L;
	private Integer id;
	private String name;
	private String password;
	
	
	
	
	
	public Client(String name,String password ) {
		this.name=name;
		this.password=password;
	}
	
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	


	public Integer getId() {
		return id;
	}


	public void setId(Integer id) {
		this.id = id;
	}


	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	//查看商品
	public static  void listAllGoods(Client c)
	{
		Scanner in=new Scanner(System.in);
		Set<Goods> list =IFactory.selectAllGoods();
		System.out.println(list);
		System.out.println("按任意键返回");
		in.next();
		clientMenu(c);
	}
	//选购商品
	public static void buyGoods(Client c)
	{
		
		Carts car=IFactory.selectABuyList(c);
		Map<Goods,Integer> map;
		if(car==null)
		{
			map=new HashMap<>();
		}else{
		map=car.getBuygood();
		}
		
		
		Scanner in=new Scanner(System.in);
		Set<Goods> list =IFactory.selectAllGoods();
		System.out.println(list);
		while(true)
		{
			//得到商品
			Goods g=null;
			while(true)
			{
				System.out.println("--------选购商品---------");
				System.out.println("请输入商品id选购");
				String a=in.next();
				try{
					
					int f=Integer.parseInt(a);
					g=IFactory.selectAGoods(f);
					if(g==null)
					{
						System.err.println("输入错误id 重新输入：");
						continue;
					}else{break;}
				}catch(Exception e){
					System.err.println("请输入数字：");
					continue;
				}				
			}
			//得到数量
			int goodsnumber=0;
			while(true)
			{
				System.out.println("输入您要买的数量");
				String p=in.next();
				try{
					goodsnumber=Integer.parseInt(p);
					break;
				}catch(Exception e){
					System.err.println("输入数字！");
					continue;
				}
			}
			//添加
			map.put(g, goodsnumber);	
			System.out.println("继续？(y/n)");
			if("y".equals(in.next()))
			{
				continue;
			}else{
				break;
			}	
		}	
		Carts cart=new Carts(map,c.getName());
		if(IFactory.addACarts(cart, c))
		{
			System.out.println("添加购物车成功");
			System.out.println(IFactory.selectABuyList(c));
			System.out.println("任意键返回");
			in.next();
			clientMenu(c);
		}else{
			System.err.println("添加失败");
		}
		
	}
	//登录
	public static void clieLogin()
	{
		System.out.println("------------用户登陆---------------");
		Scanner in=new Scanner(System.in);
		System.out.println("请输入用户名：");
		String name=in.next();
		System.out.println("请输入密码：");
		String pass=in.next();
		Client c=IFactory.selectAClient(name);
		if(c!=null)
		{
			if(c.getPassword().equals(pass))
			{
				System.out.println("登陆成功");
				clientMenu(c);
				
			}else{
				System.err.println("登陆失败    (1)重新登录?  or (2)重新选择登录方式?");
				
				while(true)
				{
					String a=in.next();
					int g=0;
					try{
						g=Integer.parseInt(a);
					}catch(Exception e){
						System.err.println("请输入数字：");
						continue;
					}
					if(g==1){
						clieLogin();break;
					}else if(g==2)
					{
						EntryMain.mainMenu();
						EntryMain.start();break;
					}else{
						System.err.println("输错数字了重新输入：");
					}
				}
			}
		}else{
			System.err.println("没有这个用户，重新输入");
			clieLogin();
		}
	}
	//用户主界面
	public static void clientMenu(Client c)
	{
		Scanner in =new Scanner(System.in);
		System.out.println("---------------------------------用户界面-------------------------------------");
		System.out.println("(1)查看所有商品	(2)选购商品	(3)查看清单 	 (4)返回登陆 界面");
		while(true)
		{
			String a=in.next();
			int g=0;
			try{
				g=Integer.parseInt(a);
			}catch(Exception e){
				System.err.println("请输入数字：");
				continue;
			}
			if(g==1){
				listAllGoods(c);break;
			}else if(g==2){
				buyGoods(c);break;
			}else if(g==3){
				System.out.println(IFactory.selectABuyList(c));
				System.out.println("任意键返回");
				in.next();
				clientMenu(c);
				break;
			}else if(g==4){
				EntryMain.mainMenu();
				EntryMain.start();break;
			}else{
				System.err.println("输错数字了重新输入：");
			}
		}
		
	}
	//注册
	public static void register()
	{
		
		Scanner in=new Scanner(System.in);
		System.out.println("----------用户注册------------");
		System.out.println("请输入用户名：");
		String name=in.next();
		System.out.println("请输入密码：");
		String pass=in.next();
		Client client=new Client(name, pass);
		boolean a=IFactory.addClient(client);
		
		if(a)
		{
			System.out.println("注册成功");
			clieLogin();
		}else{
			System.err.println("注册失败，用户名重复 ,重新输入");
			register();
		}
		
	}


	@Override
	public int hashCode() {
		
		return this.name.hashCode();
	}


	@Override
	public boolean equals(Object obj) {
		Client a=(Client)obj;
		if(a.getName().equals(this.name))
		{
			return true;
		}
		return false;
	}


	@Override
	public String toString() {
		
		return this.name+"  "+this.id+"   "+this.password;
	}
	
	
	
}
